package it.hive.controller.listener;

import it.hive.mediaplayer.beans.impl.Traccia;
import it.hive.model.common.ApplicationProperties;

import java.util.HashMap;
import java.util.Map;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;

public class TrackListener implements ChangeListener<Traccia> {
	private Map<Object, Runnable> actions;

	public TrackListener() {
		actions = new HashMap<>();
	}

	@Override
	public void changed(ObservableValue<? extends Traccia> arg0, Traccia arg1, Traccia arg2) {
		ApplicationProperties.currentSelectedTrack.set(arg2);
		eseguiAzioni();
	}

	private void eseguiAzioni() {
		for (Object object : actions.keySet()) {
			Thread t = new Thread(actions.get(object));
			t.start();
			try {
				t.join();
			} catch (InterruptedException e) {
				continue;
			}
		}
	}

	public void addAction(Object caller, Runnable action) {
		actions.put(caller, action);
	}

}
